package com.jxsm.controller;

import com.jxsm.pojo.OutproDuctprint;
import com.jxsm.service.OutProductPrintService;
import com.jxsm.util.DownloadUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/11/6.
 */
@Controller
public class OutprintController extends BaseController{
    @Autowired
    private OutProductPrintService outProductPrintService;
    @RequestMapping("/cargo/contract/outprint.action")
    public String outprint(Date shipTime){
       return "/cargo/contract/outprint.jsp";
    }
    @RequestMapping("/cargo/contract/outpring.action")
    public String printoutProduct(String outpring, HttpServletRequest request,HttpServletResponse response) throws IOException {
        String path=request.getSession().getServletContext().getRealPath("/")+"make/xlsprint/";
        Map map=new HashMap();
        map.put("inputTime",outpring);
        File file=new File(path+"outproduct.xls");
        InputStream inputStream=new FileInputStream(file);
        Workbook workbook= new HSSFWorkbook(inputStream);
        Sheet sheet=workbook.getSheetAt(0);
        Row row=null;
        Cell cell=null;
        int rowNo=0;
        int cellNo=1;
        //获取末班上的第二个单元格；
        row=sheet.getRow(1);

        //获取第一个样式
        cell=row.getCell(1);
        CellStyle customStyle=cell.getCellStyle();

        //获取第2个样式
        cell=row.getCell(2);
        CellStyle cellStyle=cell.getCellStyle();

        //获取第3个样式
        cell=row.getCell(3);
        CellStyle contractNoStyle=cell.getCellStyle();
        //获取第4个样式
        cell=row.getCell(4);
        CellStyle productNoStyle=cell.getCellStyle();
        //获取第5个样式
        cell=row.getCell(5);
        CellStyle numStyle=cell.getCellStyle();
        //获取第6个样式
        cell=row.getCell(6);
        CellStyle factoryStyle=cell.getCellStyle();
        //获取第7个样式
        cell=row.getCell(7);
        CellStyle dateStyle=cell.getCellStyle();
        //获取第8个样式
        cell=row.getCell(8);
        CellStyle tradeStyle=cell.getCellStyle();
        //处理大标题
        row = sheet.getRow(rowNo++);//获取第一行对象
//        获取第一个单元格
        cell=row.getCell(cellNo);
        cell.setCellValue(outpring.replaceFirst("-0", "-").replaceFirst("-", "年") + "月份出货表");		//yyyy-MM
        rowNo++;
        List<OutproDuctprint> list=outProductPrintService.find(map);
        for(int i=0;i<list.size();i++){
            cellNo=1;
            OutproDuctprint op=list.get(i);
           row = sheet.createRow(rowNo++);
           row.setHeightInPoints(24);

            cell = row.createCell(cellNo++);
            cell.setCellValue(op.getCustomName());
            cell.setCellStyle(customStyle);

            cell = row.createCell(cellNo++);
            cell.setCellValue(op.getContractNo());
            cell.setCellStyle(contractNoStyle);

            cell = row.createCell(cellNo++);
            cell.setCellValue(op.getProductNo());
            cell.setCellStyle(productNoStyle);

            cell = row.createCell(cellNo++);
            cell.setCellValue(op.getCnumber());
            cell.setCellStyle(numStyle);

            cell = row.createCell(cellNo++);
            cell.setCellValue(op.getFactoryName());
            cell.setCellStyle(factoryStyle);

            cell = row.createCell(cellNo++);
            cell.setCellValue(op.getDeliveryPeriod());
            cell.setCellStyle(dateStyle);

            cell = row.createCell(cellNo++);
            cell.setCellValue(op.getShipTime());
            cell.setCellStyle(dateStyle);

            cell = row.createCell(cellNo++);
            cell.setCellValue(op.getTradeTerms());
            cell.setCellStyle(tradeStyle);
        }
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        workbook.write(os);
//
        DownloadUtil downloadUtil = new DownloadUtil();				//直接弹出下载框，用户可以打开，可以保存
        downloadUtil.download(os, response, "出货表.xls");

        os.flush();
        os.close();
        return "redirect:/cargo/contract/outprint.action";
    }
}
